<template>
  <el-form ref="editFormRef" :model="form" v-loading="loading">
    <pub-edit-description :ndColumns="1">
      <pub-edit-description-item :span="1" label="用户角色" required>
        <el-form-item prop="gongRoleId" :rules="$formRules.select">
          <org-select-multiple
          v-if="isAll"
            v-model="form.gongRoleId"
            valueType="String"
            :multiple="false"
            apiUrl="/gongRole"
            :searchForm="{ pageNum: 1, pageSize: 6000 }"
          ></org-select-multiple>
          <org-select-multiple
          v-else
            v-model="form.gongRoleId"
            valueType="String"
            :multiple="false"
            apiUrl="/gongRole"
            :searchForm="{ pageNum: 1, pageSize: 6000, level:orgInfo.level, orgType:orgInfo.orgType }"
          ></org-select-multiple>
        </el-form-item>
      </pub-edit-description-item>
    </pub-edit-description>
  </el-form>
  <div class="dialog-footer-btn">
    <el-button type="default" @click="emit('close')">取消</el-button>
    <el-button
      type="primary"
      v-prevent-repeat-click
      @click="submitForm(editFormRef)"
    >
      保存
    </el-button>
  </div>
</template>

<script setup>
import { ref} from 'vue'
import { get_role_bind_detail, bind_role } from '@/api/system.js'
import { ElMessage } from 'element-plus'
import { useUserStore } from '@/pinia/modules/user'
const orgInfo = useUserStore().orgInfo
const editFormRef = ref()
const loading = ref(false)
const emit = defineEmits(['close'])
const form = ref({})
const props = defineProps({
  editId: {
    type: String
  },
  isAll:{
    type:Boolean,
    default:false
  }
})

const submitForm = () => {
  editFormRef.value.validate(async valid => {
    if (valid) {
      loading.value = true
      form.value.userId = props.editId
      let res = await bind_role(form.value)
      if (res.succeed) {
        ElMessage({
          type: 'success',
          message: res.msg
        })
      }
      loading.value = true
      emit('close')
    }
  })
}

const getDetail = async () => {
  let res = await get_role_bind_detail(props.editId)
  if (res.succeed) {
    if (res.data && res.data.length > 0) {
      form.value.gongRoleId = res.data[0].id
      form.value.id = res.data[0].gongUserRoleId
    }
  }
}
if (props.editId) {
  getDetail()
}
</script>
<style scoped></style>
